package com.huawei.esdk.uc.sametime.widgets;

import org.apache.log4j.Logger;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.huawei.esdk.uc.jna.UCService;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.util.PluginUtil;

/**
 * 工具栏上,拨号盘扩展按钮的控制类
 * @author y00175154
 * 
 */
public class SecondDialPad implements IShellProvider
{

    private Logger logger = Logger.getLogger(SecondDialPad.class);

    private Shell dialPad;

    private Shell parent;

    private Text callNumberText;// 呼叫号码输入框

    private StringBuffer inputBuffer;

    public SecondDialPad(Composite composite)
    {
        parent = composite.getShell();
        inputBuffer = new StringBuffer();
    }

    public void dispose()
    {
        if (null != dialPad)
        {
            clearInputBuffer();
            dialPad.dispose();
        }
    }

    @Override
    public Shell getShell()
    {
        if (null != dialPad)
        {
            dialPad.dispose();
        }
        dialPad = new Shell(parent, SWT.DIALOG_TRIM);
        dialPad.setText(Messages.DialPad);
        dialPad.setSize(230, 253);

        createContent(dialPad);
        return dialPad;
    }

    /**
     * Create the content.
     * @param parent
     * @param style
     */
    private void createContent(final Shell parent)
    {
        createCallNumberText(parent);

        createNumbersButton(parent);
    }

    private void createNumbersButton(final Shell parent)
    {
        Button numButton_1 = new Button(parent, SWT.NONE);
        numButton_1.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_1.setBounds(10, 56, 55, 35);
        numButton_1.setText("1");

        Button numButton_2 = new Button(parent, SWT.NONE);
        numButton_2.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_2.setText("2");
        numButton_2.setBounds(81, 56, 55, 35);

        Button numButton_3 = new Button(parent, SWT.NONE);
        numButton_3.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_3.setText("3");
        numButton_3.setBounds(151, 56, 55, 35);

        Button numButton_4 = new Button(parent, SWT.NONE);
        numButton_4.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_4.setText("4");
        numButton_4.setBounds(10, 97, 55, 35);

        Button numButton_5 = new Button(parent, SWT.NONE);
        numButton_5.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_5.setText("5");
        numButton_5.setBounds(81, 97, 55, 35);

        Button numButton_6 = new Button(parent, SWT.NONE);
        numButton_6.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_6.setText("6");
        numButton_6.setBounds(151, 97, 55, 35);

        Button numButton_7 = new Button(parent, SWT.NONE);
        numButton_7.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_7.setText("7");
        numButton_7.setBounds(10, 138, 55, 35);

        Button numButton_8 = new Button(parent, SWT.NONE);
        numButton_8.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_8.setText("8");
        numButton_8.setBounds(81, 138, 55, 35);

        Button numButton_9 = new Button(parent, SWT.NONE);
        numButton_9.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_9.setText("9");
        numButton_9.setBounds(151, 138, 55, 35);

        Button numButton_xing = new Button(parent, SWT.NONE);
        numButton_xing.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_xing.setText("*");
        numButton_xing.setBounds(10, 179, 55, 35);

        Button numButton_0 = new Button(parent, SWT.NONE);
        numButton_0.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_0.setText("0");
        numButton_0.setBounds(81, 179, 55, 35);

        Button numButton_jing = new Button(parent, SWT.NONE);
        numButton_jing.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_jing.setText("#");
        numButton_jing.setBounds(151, 179, 55, 35);

        addDialNumButtonSelectionListener(numButton_1);
        addDialNumButtonSelectionListener(numButton_2);
        addDialNumButtonSelectionListener(numButton_3);
        addDialNumButtonSelectionListener(numButton_4);
        addDialNumButtonSelectionListener(numButton_5);
        addDialNumButtonSelectionListener(numButton_6);
        addDialNumButtonSelectionListener(numButton_7);
        addDialNumButtonSelectionListener(numButton_8);
        addDialNumButtonSelectionListener(numButton_9);
        addDialNumButtonSelectionListener(numButton_xing);
        addDialNumButtonSelectionListener(numButton_0);
        addDialNumButtonSelectionListener(numButton_jing);
    }

    private void createCallNumberText(final Shell parent)
    {
        callNumberText = new Text(parent, SWT.BORDER);
        callNumberText.setText("");
        callNumberText.setBounds(10, 10, 196, 25);
        callNumberText.addModifyListener(new ModifyListener()
        {

            @Override
            public void modifyText(ModifyEvent arg0)
            {

                int oldLen = inputBuffer.length();
                int newLen = callNumberText.getText().length();
                // 针对有新输入的情况,需要做判断输入是否合法的处理
                if (newLen > oldLen)
                {
                    char lastChar = callNumberText.getText().charAt(newLen - 1);
                    // 如果不是合法的输入,则需要将最后一位去掉
                    if (!isValidInput(lastChar))
                    {

                        if (1 == newLen)
                        {
                            callNumberText.setText("");
                        }
                        else if (2 == newLen)
                        {
                            String srcStr = callNumberText.getText();
                            callNumberText.setText(srcStr.substring(0, 1));
                            callNumberText.setSelection(1);
                        }
                        else
                        {
                            String newString = callNumberText.getText()
                                    .substring(0, newLen - 1);
                            callNumberText.setText(newString);
                            callNumberText.setSelection(newString.length());
                        }
                    }
                    else
                    {
                        inputBuffer.append(lastChar);
                        // 这里需要加上二次拨号的内容
                        UCService.getInstance().UC_SDK_SendDTMF(lastChar);
                    }
                }
                // 针对删除输入的情况,需要屏蔽删除操作
                else if (newLen < oldLen)
                {
                    callNumberText.setText(inputBuffer.toString());
                    callNumberText.setSelection(oldLen);
                }
                else
                {
                    logger.debug("len is equal");
                }
            }
        });
        callNumberText.addKeyListener(new KeyListener()
        {

            @Override
            public void keyPressed(KeyEvent arg0)
            {
                logger.debug("keycode is: " + arg0.keyCode + ", char is: "
                        + arg0.character);
            }

            @Override
            public void keyReleased(KeyEvent arg0)
            {

            }
        });
    }

    private void addDialNumButtonSelectionListener(final Button num_button)
    {
        num_button.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                String dialStr = num_button.getText();
                inputBuffer.append(dialStr);
                String newStr = callNumberText.getText() + dialStr;
                callNumberText.setText(newStr);
                char dialChar = dialStr.charAt(0);
                // 这里需要加上二次拨号的内容
                UCService.getInstance().UC_SDK_SendDTMF(dialChar);
            }
        });
    }

    /**
     * 判断输入到被叫号码输入框中的字符是不是合法的
     * @param inputChar
     * @return 如果是数字,*,#,则是合法,否则不是
     */
    public boolean isValidInput(final char inputChar)
    {

        if ('*' == inputChar || '#' == inputChar)
        {
            return true;
        }
        if (inputChar >= '0' && inputChar <= '9')
        {
            return true;
        }
        return false;
    }

    private void clearInputBuffer()
    {
        int len = inputBuffer.length();
        if (len > 0)
        {
            for (int i = len - 1; i >= 0; i--)
            {
                inputBuffer.deleteCharAt(i);
            }
        }

    }
}
